package com.ebiz.plugin.alibc;

import android.app.Application;
import android.util.Log;

import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.alibaba.fastjson.JSONObject;
import com.ebiz.plugin.alibc.utils.BCResponse;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class LoginModule extends UniModule {

    String TAG = "LoginModule";


    /**
     * 初始化
     */
    @UniJSMethod(uiThread = true)
    public void init(JSONObject options, final UniJSCallback callback) {
        Log.e(TAG, "初始化 ");
        try {
            AlibcTradeSDK.asyncInit(BCInit_AppProxy.application, new AlibcTradeInitCallback() {
                @Override
                public void onSuccess() {
                    Log.e(TAG, "初始化成功");
                    JSONObject data = new JSONObject();
                    BCResponse.buildSuccessCallback(callback, "初始化成功", data);
                }

                @Override
                public void onFailure(int code, String msg) {
                    Log.e(TAG, "初始化失败：" + code + ":" + msg);
                    JSONObject data = new JSONObject();
                    data.put("code", code);
                    data.put("msg", msg);
                    BCResponse.buildFailedCallback(callback, "初始化失败", data);
                }
            });
        } catch (Throwable throwable) {
            Log.e(TAG, throwable.getMessage());
        }
    }

    //登录
    @UniJSMethod(uiThread = true)
    public void login(JSONObject options, final UniJSCallback callback) {
        Log.e(TAG, "登录");
        AlibcLogin alibcLogin = AlibcLogin.getInstance();
        alibcLogin.showLogin(new AlibcLoginCallback() {
            @Override
            public void onSuccess(int loginResult, String openId, String userNick) {
                Log.e(TAG, "登录成功");
                // 将数据返回
                JSONObject data = new JSONObject();
                data.put("loginResult", loginResult);
                data.put("openId", openId);
                data.put("userNick", userNick);
                data.put("session", AlibcLogin.getInstance().getSession());

                BCResponse.buildSuccessCallback(callback, "登录成功", data);
                // 参数说明：
                // loginResult(0--登录初始化成功；1--登录初始化完成；2--登录成功)
                // openId：用户id
                // userNick: 用户昵称
                Log.i(TAG, "获取淘宝用户信息: " + AlibcLogin.getInstance().getSession());
            }

            @Override
            public void onFailure(int code, String msg) {
                // code：错误码  msg： 错误信息
                JSONObject data = new JSONObject();
                data.put("code", code);
                data.put("msg", msg);
                Log.e(TAG, msg);
                BCResponse.buildFailedCallback(callback, "登录失败", data);
            }
        });
    }


    @UniJSMethod(uiThread = true)
    public void logout(JSONObject options, final UniJSCallback callback) {
        AlibcLogin alibcLogin = AlibcLogin.getInstance();
        alibcLogin.logout(new AlibcLoginCallback() {
            @Override
            public void onSuccess(int loginResult, String openId, String userNick) {

                // 将数据返回
                JSONObject data = new JSONObject();
                data.put("loginResult", loginResult);
                data.put("openId", openId);
                data.put("userNick", userNick);

                BCResponse.buildSuccessCallback(callback, "登出成功", data);
                // 参数说明：
                // loginResult(3--登出成功)
                // openId：用户id
                // userNick: 用户昵称
            }

            @Override
            public void onFailure(int code, String msg) {
                // code：错误码  msg： 错误信息
                JSONObject data = new JSONObject();
                data.put("code", code);
                data.put("msg", msg);
                BCResponse.buildFailedCallback(callback, "登出失败", data);
            }
        });
    }


    //run JS thread
    @UniJSMethod(uiThread = false)
    public JSONObject testSyncFunc() {
        JSONObject data = new JSONObject();
        data.put("code", "success");
        return data;
    }

}
